package com.quasar.hpatient.module.home_medicine_record;

import android.support.v7.widget.RecyclerView;
import com.quasar.hpatient.R;
import com.quasar.hpatient.api.HttpClient;
import com.quasar.hpatient.api.HttpParams;
import com.quasar.hpatient.bean.home_medicine_record.MedicineRecoedBean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import io.reactivex.Observable;
import lib.quasar.base.frame.BasePresenter;
import lib.quasar.base.frame.OnModelAcceptChangeListener;
import lib.quasar.base.http.HttpResult;
import lib.quasar.context.BaseApp;
import lib.quasar.recycler.BaseCommonAdapter;
import lib.quasar.recycler.holder.RecyclerHolder;
import lib.quasar.recycler.manager.CrashLinearLayoutManager;
import lib.quasar.util.SpannableUtil;
import okhttp3.RequestBody;

public class MedicineRecordPresenter implements BasePresenter {

    private final ArrayList<MedicineRecoedBean.MedicineRecoedItemBean> mDatas = new ArrayList<>();

    void initList(MedicineRecordView view, RecyclerView recycler) {


        final BaseCommonAdapter<MedicineRecoedBean.MedicineRecoedItemBean> adapter = new BaseCommonAdapter<MedicineRecoedBean.MedicineRecoedItemBean>(mDatas, R.layout.activity_home_medicine_record_child) {

            @Override
            protected void onNext(RecyclerHolder holder, final MedicineRecoedBean.MedicineRecoedItemBean model, final int position) {

                String editName = "";
                // 基本信息
                final MedicineRecoedBean.MedicineRecoedItemBean.MedicineRecoedContentBean model1 = model.getJson_record();
                if (null != model1) {
                    if (1 == model1.getEditortype())
                        editName = model1.getHospital()+" "+model1.getEditname();
                    else
                        editName = model1.getEditname();
                    view.setText(holder, R.id.activity_home_medicine_record_child_time, model1.getEdittime());
                    view.setText(holder, R.id.activity_home_medicine_record_child_name, editName);
                    view.setText(holder, R.id.activity_home_medicine_record_child_modify, model1.getEditcontent());
                }

                // 服药信息
                final List<MedicineRecoedBean.MedicineRecoedItemBean.MedicineRecoedSimpleContentBean> model2 = model.getJson_simple_content();
                if (null == model2 || model2.size() == 0) return;
                for (MedicineRecoedBean.MedicineRecoedItemBean.MedicineRecoedSimpleContentBean temp : model2) {
                    final String text = BaseApp.getResource().getString(R.string.home_medicine_record_list, temp.getMname(), temp.getDose());
                    SpannableUtil.append(text, BaseApp.getColors(R.color.color_font_gray60), 1f);
                    if (model2.indexOf(temp) == (model2.size() - 1)) continue;
                    SpannableUtil.newline();
                }
                view.setText(holder, R.id.activity_home_medicine_record_child_number, SpannableUtil.build());
            }
        };

        adapter.setNullView(BaseApp.getContext(), R.layout.layout_recycler_loading);
        final CrashLinearLayoutManager manager = new CrashLinearLayoutManager(BaseApp.getContext());
        view.setRecycler(recycler, adapter, manager);
    }

    void recordList(MedicineRecordView view, RecyclerView recycler) {

        final HashMap<String, Object> map = new HashMap<>();
        map.put(HttpParams.BEGIN_DATETIME, getSecond());

        final RequestBody params = createParams(map, HttpParams.DATA_TYPE_5_3);
        Observable<HttpResult<MedicineRecoedBean>> observable = HttpClient.getSingleHolder().getHttpService().medicineRecordList(params);

        request(observable, new OnModelAcceptChangeListener<MedicineRecoedBean>() {

            @Override
            public void modelFail() {
                view.setListNull(recycler, R.layout.layout_recycler_fail);
            }

            @Override
            public void modelSucc(MedicineRecoedBean model) {

                if (null == model) {
                    modelFail();
                    return;
                }

                final List<MedicineRecoedBean.MedicineRecoedItemBean> list = model.getDatas();
                if (null == list || list.size() == 0) {
                    modelFail();
                    return;
                }
                mDatas.clear();
                mDatas.addAll(list);
                view.refreshList(recycler);
            }
        });
    }

    @Override
    public void recycler() {
    }
}
